package reflect;

import java.io.File;
import java.net.URISyntaxException;

/**
 * 反射机制中获取注解信息
 */
public class ReflectDemo7 {
    public static void main(String[] args) throws URISyntaxException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        //自动实例化reflect中有@AutoRunClass标注的类
        File dir = new File(
                ReflectDemo7.class.getResource(".").toURI()
        );

        File[] subs = dir.listFiles(f->f.getName().endsWith(".class"));
        for(File sub : subs){
            String fileName = sub.getName();
            String className = fileName.substring(
                    0,fileName.indexOf("."));

            Class cls = Class.forName("reflect."+className);
//            System.out.println("获取到类:"+className);
            //判断当前类对象表示的类是否被@AutoRunClass注解标注
            if(cls.isAnnotationPresent(AutoRunClass.class)){
                System.out.println(className+"被标注了!");
                Object obj = cls.newInstance();
                System.out.println(obj);
            }

        }
    }

}





